package de.farbstreich.tozen2.client;

import java.io.BufferedWriter;
import java.io.IOException;

import de.farbstreich.tozen2.client.GameData.Player;
import de.farbstreich.tozen2.common.TozenMessageType;
import de.farbstreich.tozen2.common.TozenProtocolMessage;
import de.farbstreich.tozen2.common.TozenProtocolMessage.game;

/**
 * generates protocol messages from user(gui) Input and send to server
 * 
 * @author mo
 *
 */
public class ClientGUIInput {

	private GameData gameData;
	private ClientSocket socket;
	private ClientGUIFrame2 frame;
	
	public ClientGUIInput() {
		
	}
	
	public void setParameters(GameData gameData, ClientSocket socket, ClientGUIFrame2 frame) {
		this.gameData = gameData;
		this.socket = socket;
		this.frame = frame;
	}
	
	/**
	 * send protocol msg over stream to server
	 * @param msg
	 */
	private synchronized void sendAction(String msg) {
		System.out.println("outgoing message: "+msg);
		try {
			BufferedWriter out = getOutputStream();
			out.write(msg);
			out.newLine();
			out.flush();
		} catch (IOException e) {
			System.out.println("Fehler beim Senden von Message: " + e);
			e.printStackTrace();
		}
		
	}
	
	public BufferedWriter getOutputStream() {
		if(socket!=null)
			return socket.outputStream;
		return null;
	}

	/**
	 * connect to server and send chosen username
	 * @param serverAddress
	 * @param username
	 */
	public synchronized void setConnection(String serverAddress, String username) {
		int port = 12399;
		if(socket.isSet() || socket.startSocket(serverAddress, port)) {
			sendSetName(username);
		} else {
			ErrorMessage.showErrorMessage(frame.login.getConnectionButton(), "Fehler beim Verbinden zum Server.");
			System.out.println("Fehler beim Verbinden zum Server.");
		}
	}

	/**
	 * send chosen name
	 * 
	 * @protocol SET_NAME
	 * @param username
	 */
	public synchronized void sendSetName(String username) {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.SET_NAME);
		TozenProtocolMessage.player p = protocolMessage.new player();
		p.name = username;
		protocolMessage.players.add(p);
		sendAction(protocolMessage.toString());
	}

	/**
	 * send chat message
	 * 
	 * @protocol MESSAGE
	 * @param message
	 * @param player
	 */
	public void sendMessage(String message, Player player) {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.MESSAGE);
		protocolMessage.message = message;
		TozenProtocolMessage.player p = protocolMessage.new player();
		p.id = player.playerId;
		protocolMessage.players.add(p);
		sendAction(protocolMessage.toString());
	}

	/**
	 * send name and type of new created game
	 * 
	 * @protocol SET_GAME
	 * @param gameType
	 * @param name
	 */
	public synchronized void sendSetGame(int gameType, String name) {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.SET_GAME);
		TozenProtocolMessage.game g = protocolMessage.new game();
		g.name = name;
		g.id = gameType;
		protocolMessage.games = new game[1];
		protocolMessage.games[0] = g;
		//System.out.println("debug(createNewGame): "+protocolMessage.toString());
		sendAction(protocolMessage.toString());
	}
	
	/**
	 * join to game with id
	 * 
	 * @protocol JOIN_GAME
	 * @param gameId
	 */
	public void sendJoinGame(int gameId) {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.JOIN_GAME);
		TozenProtocolMessage.game g = protocolMessage.new game();
		g.id = gameId;
		protocolMessage.games = new game[1];
		protocolMessage.games[0] = g;
		sendAction(protocolMessage.toString());
	}

	/**
	 * start game - only by gameadmin
	 * @protocol START_GAME
	 */
	public void sendStartGame() {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.START_GAME);
		sendAction(protocolMessage.toString());
	}
	
	/**
	 * send move to server for validity check
	 * @protocol TEST_MOVE
	 * @param startX
	 * @param startY
	 * @param endX
	 * @param endY
	 */
	public void sendTestMove(int startX, int startY, int endX, int endY) {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.TEST_MOVE);
		protocolMessage.startX = startX;
		protocolMessage.startY = startY;
		protocolMessage.targetX = endX;
		protocolMessage.targetY = endY;
		sendAction(protocolMessage.toString());
	}
	
	/**
	 * exit running game
	 * @protocol SURRENDER_GAME
	 */
	public void sendSurrenderGame() {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.SURRENDER_GAME);
		TozenProtocolMessage.player p = protocolMessage.new player();
		protocolMessage.players.add(p);
		sendAction(protocolMessage.toString());
	}
	
	/**
	 * logout on server
	 * @protocol LOGOUT
	 */
	public void sendLogout() {
		TozenProtocolMessage protocolMessage = new TozenProtocolMessage(TozenMessageType.LOGOUT);
		sendAction(protocolMessage.toString());
		socket.closeSocket();
	}

	

	/**
	 * @deprecated
	 * @param msg
	 */
	public void makeMeHappy(String msg) {
		
	}

	

	
	
}
